package net.cyl.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Data
public class RabbitMQConfig {

    private String orderEventExchange= "order-event-exchange";

    private String orderCloseDelayQueue = "order.close.delay.queue";


    private String orderCloseQueue = "order.close.queue";

    private String orderCloseDelayRoutingKey = "order.close.delay.routing.key";

    private String orderCloseRoutingKey = "order.close.routing.key";

    private Integer ttl = 60000;
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(orderEventExchange, true, false);
    }
    @Bean
    public Queue orderCloseDelayQueue() {
        // 创建一个Map，用于存储队列的参数
        Map<String, Object> arguments = new HashMap<>();
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange", orderEventExchange);
        // 设置死信路由键
        arguments.put("x-dead-letter-routing-key", orderCloseRoutingKey);
        // 设置消息的TTL
        arguments.put("x-message-ttl", ttl);
        // 返回一个队列对象
        return new Queue(orderCloseDelayQueue, true, false, false, arguments);
    }
    @Bean
    public Queue orderCloseQueue() {
        return new Queue(orderCloseQueue, true, false, false);
    }
    @Bean
    public Binding orderCloseDelayQueueBinding() {
        return new Binding(orderCloseDelayQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderCloseDelayRoutingKey, null);
    }
    @Bean
    public Binding orderCloseQueueBinding() {
        return new Binding(orderCloseQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderCloseRoutingKey, null);

    }

    //=========订单支付成功配置=============

    private String orderUpdateQueue = "order.update.queue";

    private String orderTrafficQueue = "order.traffic.queue";



    private String orderUpdateTrafficRoutingKey = "order.update.traffic.routing.key";



    private String orderUpdateBindingKey = "order.update.*.routing.key";


    private String orderTrafficBindingKey = "order.*.traffic.routing.key";


    @Bean
    public Binding orderUpdateQueueBinding() {
        return new Binding(orderUpdateQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderUpdateBindingKey, null);
    }

    @Bean
    public Binding orderTrafficQueueBinding() {
        return new Binding(orderTrafficQueue, Binding.DestinationType.QUEUE, orderEventExchange, orderTrafficBindingKey, null);
    }

    @Bean
    public Queue orderUpdateQueue() {
        return new Queue(orderUpdateQueue, true, false, false);
    }

    @Bean
    public Queue orderTrafficQueue() {
        return new Queue(orderTrafficQueue, true, false, false);
    }
}
